package com.workflow.workflowproject.until;

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.impl.de.odysseus.el.ExpressionFactoryImpl;
import org.flowable.common.engine.impl.de.odysseus.el.ObjectValueExpression;
import org.flowable.common.engine.impl.de.odysseus.el.util.SimpleContext;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.common.engine.impl.javax.el.ValueExpression;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ExpressionCmd implements Command<Boolean>, Serializable {

    protected RuntimeService runtimeService;

    protected ProcessEngineConfigurationImpl processEngineConfiguration;

    protected String processInstanceId;

    protected String exp;

    protected Map<String, Object> variableMap;

    private Map<String, String> operatorMap = new HashMap<>();

    {
        // 等于
        operatorMap.put("var:eq", "eq");
        // 不等于
        operatorMap.put("var:notEquals", "ne");
        // 包含
        operatorMap.put("var:containsAny", "in");
        // 不包含
        operatorMap.put("var:notContainsAny", "ni");
        // 为空
        operatorMap.put("var:isNull", "ul");
        // 不为空
        operatorMap.put("var:isNotNull", "nu");
        // 字符包含
        operatorMap.put("var:contains", "lk");
        // 大于
        operatorMap.put("var:gt", " > ");
        // 小于
        operatorMap.put("var:lt", " < ");
        // 小于或等于
        operatorMap.put("var:lte", " <= ");
        // 大于或等于
        operatorMap.put("var:gte", " >= ");
    }

    public ExpressionCmd(RuntimeService runtimeService, ProcessEngineConfigurationImpl processEngineConfiguration, String processInstanceId, String exp, Map<String, Object> variableMap) {
        this.runtimeService = runtimeService;
        this.processEngineConfiguration = processEngineConfiguration;
        this.processInstanceId = processInstanceId;
        this.exp = exp;
        this.variableMap = variableMap;
    }

    @Override
    public Boolean execute(CommandContext commandContext) {
        try {
            System.err.println(this.exp);
            System.out.println("EL表达式>>>>>:" + this.exp);
            if (StringUtils.isNotBlank(this.exp)) {
                ExpressionFactoryImpl expressionFactory = new ExpressionFactoryImpl();
                SimpleContext simpleContext = new SimpleContext();
                for (String key : variableMap.keySet()) {
                    ObjectValueExpression valueExpression = expressionFactory.createValueExpression(variableMap.get(key), Object.class);
                    simpleContext.setVariable(key, valueExpression);
                }
                if (variableMap.get("projectId") == null) {
                    simpleContext.setVariable("projectId", expressionFactory.createValueExpression("", Object.class));
                }
                if (variableMap.get("departmentId") == null) {
                    simpleContext.setVariable("departmentId", expressionFactory.createValueExpression("", Object.class));
                }
                ValueExpression valueExpression1 = expressionFactory.createValueExpression(simpleContext, this.exp, Boolean.class);
                boolean value = (boolean) valueExpression1.getValue(simpleContext);
                // true
                System.out.println("结果>>>>>:" + value);
                return value;
            }
        }catch (Exception e){
        }
        return true;
    }
}
